Component({
    properties: {
        data: {
            type: Object,
            value: {}
        },
        calShow: {
            type: Boolean,
            value: false
        },
        calTarget: {
            type: String,
            value: ''
        },
        param: {
            type: Object,
            value: {}
        },
        minDate: {
            type: String,
            value: new Date().getTime() - 180 * 24 * 3600 * 1000
        },
        maxDate: {
            type: String,
            value: new Date().getTime() + 180 * 24 * 3600 * 1000
        }
    },
    data: {},
    methods: {
        onConfirm(event) {
            const {value, index} = event.detail
            const that = this
            let param = this.data.param,
                array = {}
            if (Object.keys(param).length <= 0) {
                // wx.showToast({
                //     title: '请选择筛选条件'
                // })
                // return false
                for (const key in this.data.data) {
                    array[key] = ''
                }
            } else {
                for (const key in param) {
                    array[key] = (typeof (param[key]) === 'object') ? param[key]['value'] : param[key]
                }
            }

            // param
            that.triggerEvent('callback', {
                act: '确定',
                param: array
            })
        },
        onCancel(event) {
            this.triggerEvent('callback', {
                act: '取消'
            })
        },
        // 重置元素
        onReset() {
            this.setData({
                param: {}
            })
        },
        // tag选中
        onSelect(obj) {
            const {field, value} = obj.currentTarget.dataset
            if (value === '自定义') {
                this.setData({
                    calShow: true,
                    calTarget: field,
                })
            }
            this.setData({
                ['param.' + field]: value
            })
        },
        onDisplay() {
            this.setData({
                calShow: true
            });
        },
        onCalClose() {
            this.setData({
                calShow: false
            });
        },
        // 日期选择
        onCalConfirm(event) {
            const field = event.currentTarget.dataset.target
            const [start, end] = event.detail
            this.setData({
                calShow: false,
                ['param.' + field + '.value']: `${this.formatDate(start)}~${this.formatDate(end)}`,
            });
            console.log(this.data.param)
        },
        formatDate(date) {
            date = new Date(date);
            return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
        },

    },
    observers: {
        'data'(val) {
            // console.log(val)
        },
    }
});
